package top.kaoshanji.p2ch15a;

/**
 * 代码清单-p2ch15a-443页a：使用单独对象作为锁的Counter类
 * author kaoshanji
 * ^_^
 * create time 2025/2/23 18:16
 */
public class Counter3 {

    // 实例属性
    private int count;

    // 任意实例对象作为锁
    private Object lock = new Object();

    public void incr() {
        synchronized (lock) {
            count++;
        }
    }

    public int getCount() {
        synchronized (lock) {
            return count;
        }
    }

}
